//
//  XKHomeAdvanceView.m
//  XKJY
//
//  Created by xiaokang100 on 2017/1/18.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKHomeAdvanceView.h"


@interface XKHomeAdvanceView () <UITextFieldDelegate>

// 白背景
@property (nonatomic, strong) UIView *bgViewOne;
@property (nonatomic, strong) UIView *bgViewTwo;
@property (nonatomic, strong) UIView *bgViewThree;
@property (nonatomic, strong) UIView *bgViewFour;

// 提示标题
@property (nonatomic, strong) UILabel *bankAccountTitleLabel;
@property (nonatomic, strong) UILabel *advanceCountTitleLabel;
@property (nonatomic, strong) UILabel *tradePwdTitleLabel;
@property (nonatomic, strong) UILabel *codeTitleLabel;

// 输入框
@property (nonatomic, strong) UITextField *advanceCountTextField;
@property (nonatomic, strong) UITextField *tradePwdTextField;
@property (nonatomic, strong) UITextField *codeTextField;

// 获取验证码按钮
@property (nonatomic, strong) UIButton *getCodeBtn;
// 选择银行卡按钮
@property (nonatomic, strong) UIButton *bankCardNumberBtn;// 点击跳转至银行卡列表
// 确认提现按钮
@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, strong) UILabel *remindLabel;

@end


@implementation XKHomeAdvanceView

#pragma mark - 重写构造方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews];
    }
    return self;
}

#pragma mark - 选择银行卡
- (void)bankCardSelected:(UIButton *)sender {
    [self endEditing:YES];
    if (self.bankCardSelectBlock) {
        self.bankCardSelectBlock(sender);
    }
    
}

#pragma mark - 提现
- (void)confirmWithdraw:(UIButton *)sender {
    
    if (self.advanceCountTextField.text.doubleValue < 100) {
        [SVProgressHUD showErrorWithStatus:@"单笔提现额度不能低于100元!"];
        return;
    }
    
    if (self.withdrawBlock) {
        self.withdrawBlock(self.bank_id, self.advanceCountTextField.text, self.tradePwdTextField.text);
    }
}



- (void)advance_textFieldDidChange:(UITextField *)textField {
    
    if (self.tradePwdTextField.text.length == 6 &&
        self.advanceCountTextField.text.length > 0 &&
        self.bank_id.length != 0) {
        
        self.confirmBtn.enabled = YES;
    } else {
        self.confirmBtn.enabled = NO;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    if (textField == self.advanceCountTextField) {
        
        return [text isTradePrice];
        
    } else if (textField == self.tradePwdTextField) {
//        return text.length > 6 ? NO : YES;
        return [text isTradePassword];
    }
    
    return YES;
}

#pragma mark - 创建子控件
- (void)addSubviews {
    
    self.bgViewOne = [UIView new];
    self.bgViewOne.backgroundColor = WhiteColor;
    
    self.bgViewTwo = [UIView new];
    self.bgViewTwo.backgroundColor = WhiteColor;
    
    self.bgViewThree = [UIView new];
    self.bgViewThree.backgroundColor = WhiteColor;
    

    
    self.bankAccountTitleLabel  = [UILabel getLabWithText:@"到账银行卡" textFont:Font_15 textColor:BlackColor];
    self.advanceCountTitleLabel = [UILabel getLabWithText:@"提现金额" textFont:Font_15 textColor:BlackColor];
    self.tradePwdTitleLabel     = [UILabel getLabWithText:@"交易密码" textFont:Font_15 textColor:BlackColor];

    

    self.advanceCountTextField                  = [[UITextField alloc] init];
    self.advanceCountTextField.font             = Font(15);
    self.advanceCountTextField.keyboardType     = UIKeyboardTypeDecimalPad;
    self.advanceCountTextField.placeholder      = @"请输入提现金额";
    self.advanceCountTextField.textColor        = [UIColor blackColor];
    self.advanceCountTextField.delegate         = self;
    self.advanceCountTextField.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.advanceCountTextField.borderStyle      = UITextBorderStyleNone;
    [self.advanceCountTextField addTarget:self
                                   action:@selector(advance_textFieldDidChange:)
                         forControlEvents:UIControlEventEditingChanged];
    
    self.tradePwdTextField                  = [[UITextField alloc] init];
    self.tradePwdTextField.font             = Font(15);
    self.tradePwdTextField.keyboardType     = UIKeyboardTypeNumberPad;
    self.tradePwdTextField.placeholder      = @"请输入交易密码";
    self.tradePwdTextField.textColor        = [UIColor blackColor];
    self.tradePwdTextField.delegate         = self;
    self.tradePwdTextField.secureTextEntry  = YES;
    self.tradePwdTextField.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.tradePwdTextField.borderStyle      = UITextBorderStyleNone;
    [self.tradePwdTextField addTarget:self
                                   action:@selector(advance_textFieldDidChange:)
                         forControlEvents:UIControlEventEditingChanged];

    
    
    
    self.bankCardNumberBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.bankCardNumberBtn.titleLabel.font = Font(15);
    self.bankCardNumberBtn.backgroundColor = ClearColor;
    self.bankCardNumberBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self.bankCardNumberBtn setTitle:@"点击选择银行卡" forState:UIControlStateNormal];
    [self.bankCardNumberBtn setTitleColor:LightGrayColor forState:UIControlStateNormal];
    [self.bankCardNumberBtn addTarget:self action:@selector(bankCardSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //获取验证码
    self.getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.getCodeBtn.tag = 20000;
    [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.getCodeBtn setTitleColor:BlueColor forState:UIControlStateNormal];
    [self.getCodeBtn setTitleColor:LightGrayColor forState:UIControlStateDisabled];
    self.getCodeBtn.titleLabel.font     = Font(15);
    self.getCodeBtn.backgroundColor     = ClearColor;;
    self.getCodeBtn.layer.cornerRadius  = 5.0;
    self.getCodeBtn.layer.masksToBounds = YES;
    self.getCodeBtn.layer.borderWidth   = 1*Scale_W;
    self.getCodeBtn.layer.borderColor   = BlueColor.CGColor;
    
    
    self.confirmBtn = [UIButton getClickBtnWithTitle:@"确认提现"];
    [self.confirmBtn addTarget:self action:@selector(confirmWithdraw:) forControlEvents:UIControlEventTouchUpInside];
    
    self.remindLabel = [UILabel getLabWithText:[NSString stringWithFormat:@"说明:\n\t1. 提现手续费2%%,请仔细确认后再操作。\n\t2. 单笔提现限额100—50000元。\n\t3. 单日累计最大提现限额500000元。"] textFont:Font_13 textColor:RedColor textAlignment:NSTextAlignmentLeft numberOfLines:0];
    
    [self addSubview:self.bgViewOne];
    [self addSubview:self.bgViewTwo];
    [self addSubview:self.bgViewThree];

    
    [self addSubview:self.bankAccountTitleLabel];
    [self addSubview:self.advanceCountTitleLabel];
    [self addSubview:self.tradePwdTitleLabel];

    
    [self addSubview:self.advanceCountTextField];
    [self addSubview:self.tradePwdTextField];

    
    [self addSubview:self.getCodeBtn];
    [self addSubview:self.confirmBtn];
    [self addSubview:self.bankCardNumberBtn];
    

    // 在添加子控件之后再添加约束
    [self addConstraints];
}

#pragma mark - 添加约束
- (void)addConstraints {
    
    [self.bgViewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(20*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.bankAccountTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.bgViewOne);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.bgViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgViewOne.mas_bottom).offset(20*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.advanceCountTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.bgViewTwo);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.advanceCountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.advanceCountTitleLabel.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.advanceCountTitleLabel);
        make.right.mas_equalTo(self).offset(-40*Scale_W);
    }];
    
    [self.bgViewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgViewTwo.mas_bottom).offset(1*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.tradePwdTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(40*Scale_W);
        make.centerY.mas_equalTo(self.bgViewThree);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.tradePwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.tradePwdTitleLabel.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.tradePwdTitleLabel);
        make.right.mas_equalTo(self).offset(-40*Scale_W);
    }];
    
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgViewThree.mas_bottom).offset(20*Scale_H);
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
        make.height.mas_equalTo(44*Scale_H);
    }];
    
    [self.bankCardNumberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(-30*Scale_W);
        make.left.mas_equalTo(self.bankAccountTitleLabel.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.bgViewOne);
    }];
}

- (void)setCardSelecteBtnText:(NSString *)cardSelecteBtnText {
    _cardSelecteBtnText = cardSelecteBtnText;
    
    [self.bankCardNumberBtn setTitle:cardSelecteBtnText forState:UIControlStateNormal];
    [self.bankCardNumberBtn setTitleColor:BlueColor forState:UIControlStateNormal];
}

- (void)setBank_id:(NSString *)bank_id {
    _bank_id = bank_id;
    
    if (self.advanceCountTextField.text.length > 0 && self.tradePwdTextField.text > 0) {
        self.confirmBtn.enabled = YES;
    } else {
        self.confirmBtn.enabled = NO;
    }
}
@end
